package com.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.db.CommentDao;

@WebServlet(name = "CommentServlet")

public class CommentServlet extends HttpServlet{
	
	private void printContent(PrintWriter out) {
    }
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
		 resp.setContentType("text/html;charset=\"utf-8\"");
	     PrintWriter out = resp.getWriter();
	     String method = req.getParameter("method");
	     
	        //处理用户的评论
	        if(method.equals("comment")){

	            try {

	                //获取从评论传递过来的参数值
	                String ID=new String(req.getParameter("ID").getBytes("ISO-8859-1"),"UTF-8");
	                int passageID=new Integer(req.getParameter("passage_id"));
	                String msgName = new String(req.getParameter("name").getBytes("ISO-8859-1"),"UTF-8");
	                String msgContent = new String(req.getParameter("message_content").getBytes("ISO-8859-1"),"UTF-8");
	                String msgDate = new String(req.getParameter("message_time"));


	                CommentDao connect = new CommentDao();


	                if(ID.equals(""))
	                {
	                    resp.getWriter().print("<script> alert('请先登录！');location.href=\"login.jsp\"; </script>");
	                }

	                int result = connect.message(ID,msgName,msgDate,msgContent,passageID);
	                if (result == 0) {

	                    resp.getWriter().print("<script> alert('评论成功！');location.href=\"travelDetails.jsp?passageID="+ passageID +"&page=1\";</script>");
	                }
	                if (result == 1) {
	                    resp.getWriter().print("<script> alert('error！'); location.href=\"index.jsp\";</script>");
	                }


	            } catch (ClassNotFoundException e) {
	                e.printStackTrace();
	            } catch (IllegalAccessException e) {
	                e.printStackTrace();
	            } catch (InstantiationException e) {
	                e.printStackTrace();
	            } catch (SQLException e) {
	                e.printStackTrace();
	            } catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}


	        }
	     
	     
	     
	   //用于处理点赞功能的按钮
	        if(method.equals("like"))
	        {
	            if(req.getParameter("userID") == null)
	            {
	                resp.getWriter().print("<script>alert('请先登录！');location.href='login.jsp';</script>");
	            }
	            String userID = new String(req.getParameter("userID").getBytes("ISO-8859-1"),"UTF-8");
	            int passageID = new Integer(req.getParameter("passageID"));
	            CommentDao connct = new CommentDao();

	            try {
	                boolean successAdd = connct.like(passageID,userID);

	                if(successAdd == false)
	                {
	                    resp.getWriter().print("<script> alert('不能重复点赞！');location.href='travelDetails.jsp?passageID="+passageID+"&page=1';</script>");
	                }
	                if(successAdd == true)
	                {
	                    resp.getWriter().print("<script> alert('点赞成功！');location.href='travelDetails.jsp?passageID="+passageID+"&page=1';</script>");
	                }
	            } catch (ClassNotFoundException e) {
	                e.printStackTrace();
	            } catch (IllegalAccessException e) {
	                e.printStackTrace();
	            } catch (InstantiationException e) {
	                e.printStackTrace();
	            } catch (SQLException e) {
	                e.printStackTrace();
	            } catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
	        }
	}
	
	@Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        doPost(req, resp);
    }
}
